package casa.depesos.app.com.frag;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.common.base.utils.WeakHandler;

import casa.depesos.app.com.activity.BaseActivity;

public abstract class BaseFragment extends Fragment {

    protected WeakHandler h = new WeakHandler();

    protected void showLoading() {
        try {
            ((BaseActivity) getActivity()).showLoading();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    protected void hideLoading() {
        try {
            ((BaseActivity) getActivity()).hideLoading();
        } catch (Throwable e) {

        }
    }

    protected boolean isLoading() {
        try {
            return ((BaseActivity) getActivity()).isLoading();
        } catch (Throwable e) {
            return false;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        h.removeCallbacksAndMessages(null);
    }

    public void replaceFragment(int id, Fragment fragment) {
        try {
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.replace(id, fragment);
            transaction.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void clearFragment() {
        try {
            ((BaseActivity) getActivity()).clearFragment(getChildFragmentManager());
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
